/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "光追后端.h"
#include "底层绘图/底层绘图框架.h"
#include "shader_std.h"

#include <文件/file_文件名数据.h>





C_Vk光追后端::C_Vk光追后端(S_设备环境& ctx) {
	m_Ctx = &ctx;

	m_光追管线属性 = {};
	m_光追管线属性.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_PROPERTIES_KHR;

	VkPhysicalDeviceProperties2 deviceProperties2{};
	deviceProperties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
	deviceProperties2.pNext = &m_光追管线属性;

	vkGetPhysicalDeviceProperties2((VkPhysicalDevice)(m_Ctx->m_物理设备.设备), &deviceProperties2);


	//S_绘图设备配置* 绘图环境 = (S_绘图设备配置*)f_draw_创建绘图设备(ctx);
	auto* 绘图环境 = f_vk_get绘图环境(ctx);
	m_绘图环境 = (S_结构对象指针)绘图环境;

	
	m_加速结构创建命令缓存 = (S_结构对象指针)f_vk创建命令缓存(绘图环境, 1, true);
	m_渲染命令缓存 = (S_结构对象指针)f_vk创建命令缓存(绘图环境, 1, true);
	m_渲染Fence = (S_结构对象指针)f_vk创建Fence(绘图环境);
	//f_创建渲染环境()
	

	m_渲染纹理 = f_tex_创建存储2D纹理(ctx, { 1024,1024,1 }, E_纹理格式::e_tf_RGBA8U_U, E_纹理用途::e_贮存 | E_纹理用途::e_转移源);
	
	//m_渲染纹理 = f_tex_创建存储2D纹理(ctx, { 1024,1024,1 }, E_纹理格式::e_tf_RGBA32, E_纹理用途::e_贮存 | E_纹理用途::e_转移源);
	//m_底层加速结构缓存 = f_buf_create板载缓存(*m_Ctx, 1, E_板载缓存类型::e_光追底层加速结构, 1);
	m_顶层加速结构 = f_buf_create板载缓存(*m_Ctx, 1, E_板载缓存类型::e_光追顶层加速结构, 1);
	

	m_顶层实例加速结构 = f_buf_create板载缓存(*m_Ctx, sizeof(VkAccelerationStructureInstanceKHR), E_板载缓存类型::e_光追实例, 1);
	m_底层暂存缓冲区 = f_buf_create板载缓存(*m_Ctx, E_板载缓存类型::e_SSBO, E_板载缓存类型::e_SSBO | E_板载缓存类型::e_着色端内存地址, 1, 1);
	m_顶层暂存缓冲区 = f_buf_create板载缓存(*m_Ctx, E_板载缓存类型::e_SSBO, E_板载缓存类型::e_SSBO | E_板载缓存类型::e_着色端内存地址, 1, 1);


	m_生成着色列表 = f_buf_create板载缓存(*m_Ctx, 1, E_板载缓存类型::e_着色列表, 1);
	m_忽略着色列表 = f_buf_create板载缓存(*m_Ctx, 1, E_板载缓存类型::e_着色列表, 1);
	m_相交着色列表 = f_buf_create板载缓存(*m_Ctx, 1, E_板载缓存类型::e_着色列表, 1);
	m_回调着色列表 = f_buf_create板载缓存(*m_Ctx, 1, E_板载缓存类型::e_着色列表, 1);

	{
		
		S_Material 默认材质;
		默认材质.m_shaderIndex = -1;

		默认材质.m_color = {1,0,0.8};
		默认材质.m_colorTex = -1;

		默认材质.m_emission = {};			//发光
		默认材质.m_emissionTex = -1;

		默认材质.m_reflect = 0;					//反射度
		默认材质.m_reflectTex = -1;

		默认材质.m_refract = 0;					//折射
		默认材质.m_refractTex = -1;

		默认材质.m_roughness = 0.5;				//粗糙度
		默认材质.m_roughnessTex = -1;

		默认材质.m_alpha = 1;
		默认材质.m_alphaTex = -1;

		默认材质.m_normalTex = -1;
		默认材质.m_shaderIndex = -1;

		默认材质.m_transmission = 0;

		m_材质 = f_buf_create板载缓存(*m_Ctx, E_板载缓存类型::e_SSBO, E_板载缓存类型::e_SSBO | E_板载缓存类型::e_着色端内存地址 | E_板载缓存类型::e_光追底层加速结构, sizeof(S_Material), 1);
		f_buf_fill(m_材质, &默认材质, 1);
		//S_光追后端::m_材质2 = f_buf_create板载缓存(*m_Ctx, sizeof(S_Material), E_板载缓存类型::e_SSBO, 1);
		//f_buf_fill(S_光追后端::m_材质2, &默认材质, 1);
		//m_纹理 = f_buf_create板载缓存(*m_Ctx, sizeof(uint32), E_板载缓存类型::e_UBO, 1);
		m_几何属性 = f_buf_create板载缓存(*m_Ctx, sizeof(S_Geom), E_板载缓存类型::e_SSBO, 1);

		m_全局绑定参数指针 = f_buf_create板载缓存(*m_Ctx, E_板载缓存类型::e_SSBO, E_板载缓存类型::e_SSBO | E_板载缓存类型::e_着色端内存地址, sizeof(S_GlobalParam), 1);
		m_I32动态属性      = f_buf_create板载缓存(*m_Ctx, E_板载缓存类型::e_SSBO, E_板载缓存类型::e_SSBO | E_板载缓存类型::e_着色端内存地址, sizeof(int32), 1);
		m_F32动态属性      = f_buf_create板载缓存(*m_Ctx, E_板载缓存类型::e_SSBO, E_板载缓存类型::e_SSBO | E_板载缓存类型::e_着色端内存地址, sizeof(float32), 1);
		m_Vec2动态属性     = f_buf_create板载缓存(*m_Ctx, E_板载缓存类型::e_SSBO, E_板载缓存类型::e_SSBO | E_板载缓存类型::e_着色端内存地址, sizeof(vec2), 1);
		m_Vec3动态属性 = f_buf_create板载缓存(*m_Ctx, E_板载缓存类型::e_SSBO, E_板载缓存类型::e_SSBO | E_板载缓存类型::e_着色端内存地址, sizeof(vec3), 1);

		m_UVec2动态属性 = f_buf_create板载缓存(*m_Ctx, E_板载缓存类型::e_SSBO, E_板载缓存类型::e_SSBO | E_板载缓存类型::e_着色端内存地址, sizeof(uvec2), 1);
		m_UVec3动态属性 = f_buf_create板载缓存(*m_Ctx, E_板载缓存类型::e_SSBO, E_板载缓存类型::e_SSBO | E_板载缓存类型::e_着色端内存地址, sizeof(uvec3), 1);
		m_UVec4动态属性     = f_buf_create板载缓存(*m_Ctx, E_板载缓存类型::e_SSBO, E_板载缓存类型::e_SSBO | E_板载缓存类型::e_着色端内存地址, sizeof(uvec4), 1);

		m_Mat44动态属性 = f_buf_create板载缓存(*m_Ctx, E_板载缓存类型::e_SSBO, E_板载缓存类型::e_SSBO | E_板载缓存类型::e_着色端内存地址, sizeof(Mat44f), 1);

		m_采样曲线属性 = f_buf_create板载缓存(*m_Ctx, E_板载缓存类型::e_SSBO, E_板载缓存类型::e_SSBO | E_板载缓存类型::e_着色端内存地址, sizeof(S_CurvePoint), 1);

		m_I32动态属性->m_Size = 0;
		m_F32动态属性->m_Size = 0;
		m_Vec2动态属性->m_Size = 0;
		m_Vec3动态属性->m_Size = 0;

		m_UVec2动态属性->m_Size = 0;
		m_UVec3动态属性->m_Size = 0;
		m_UVec4动态属性->m_Size = 0;

		


		m_平行光缓冲区 = f_buf_create板载缓存(*m_Ctx, E_板载缓存类型::e_SSBO, E_板载缓存类型::e_SSBO | E_板载缓存类型::e_着色端内存地址, sizeof(S_LigthParallel), 1);
		m_点光源缓冲区 = f_buf_create板载缓存(*m_Ctx, E_板载缓存类型::e_SSBO, E_板载缓存类型::e_SSBO | E_板载缓存类型::e_着色端内存地址, sizeof(S_LigthPoint), 1);
		m_区域光缓冲区 = f_buf_create板载缓存(*m_Ctx, E_板载缓存类型::e_SSBO, E_板载缓存类型::e_SSBO | E_板载缓存类型::e_着色端内存地址, sizeof(S_LigthArea), 1);
		m_聚光灯缓冲区 = f_buf_create板载缓存(*m_Ctx, E_板载缓存类型::e_SSBO, E_板载缓存类型::e_SSBO | E_板载缓存类型::e_着色端内存地址, sizeof(S_LigthSpot), 1);
		m_平行光缓冲区->m_Size = 0;
		m_点光源缓冲区->m_Size = 0;
		m_区域光缓冲区->m_Size = 0;
		m_聚光灯缓冲区->m_Size = 0;

		



		m_帧渲染信息 = f_buf_create板载缓存(*m_Ctx, sizeof(GLSL_RenderFrame), E_板载缓存类型::e_UBO, 2);
		m_mapPtr_渲染帧数据 = f_buf_map板载缓存<GLSL_RenderFrame>(m_帧渲染信息);
		m_mapPtr_渲染帧数据->m_Frame = 0;
		m_mapPtr_渲染帧数据->m_ParallelLightNum = 0;
		m_mapPtr_渲染帧数据->m_SpotLightNum = 0;
		m_mapPtr_渲染帧数据->m_PointLightNum = 0;
		m_mapPtr_渲染帧数据->m_AreaLightNum = 0;
		m_mapPtr_渲染帧数据->m_BouncesNum = 4;
		m_mapPtr_渲染帧数据->m_SamplesNum = 4;
		m_mapPtr_渲染帧数据->m_world_ShaderID = -1;

		m_mapPtr_渲染帧数据->m_ViewOffset_X = 0;
		m_mapPtr_渲染帧数据->m_ViewOffset_Y = 0;



		m_场景物体网格指针 = f_buf_create板载缓存(*m_Ctx, sizeof(S_ObMesh), E_板载缓存类型::e_SSBO, 0);
		
	}
	

	
	{
		m_区域光物体网格 = f_surface_创建网格对象(0, ctx, E_网格数据枚举::e_Mesh_VULKAN);

		std::vector<S_VNT1> 顶点;
		std::vector<uint32> 索引;

		顶点.push_back({ {-1,0,-1}, {0,-1,0}, {0,0} });
		顶点.push_back({ {1,0,-1}, {0,-1,0}, {0,0} });
		顶点.push_back({ {1,0,1}, {0,-1,0}, {0,0} });
		顶点.push_back({ {-1,0,1}, {0,-1,0}, {0,0} });
		索引.push_back(0);
		索引.push_back(1);
		索引.push_back(2);
		索引.push_back(2);
		索引.push_back(3);
		索引.push_back(0);


		uint32 num = 128;
		std::vector<vec2> 圆顶点(num);
		f_graph_计算圆坐标XY({}, 1.0, num, 圆顶点.data());
		
		顶点.push_back({ {0, 0, 0}, {0,-1,0}, {0,1} });
		for (auto& e : 圆顶点) {
			顶点.push_back({ {e.x, 0, e.y}, {0,-1,0}, {0,0} });
		}


		uint32 index = 5;
		for (uint32 i = 0; i < num; ++i) {
			索引.push_back(4);
			索引.push_back(index);
			索引.push_back(++index);
		}
		索引.back() = 5;

		f_surface_fill顶点数据(m_区域光物体网格, 顶点);
		f_surface_fill索引数据(m_区域光物体网格, 索引);
	}

}

C_Vk光追后端::~C_Vk光追后端() {
	auto* 绘图配置 = (S_绘图设备配置*)m_绘图环境;
	//auto* 绘图配置 = f_vk_get绘图环境(*m_Ctx);

	f_buf_unmap板载缓存(m_帧渲染信息);
	

	f_buf_release板载缓存(m_顶层暂存缓冲区);
	f_buf_release板载缓存(m_底层暂存缓冲区);
	f_buf_release板载缓存(m_顶层加速结构);
	//f_buf_release板载缓存(m_底层加速结构缓存);
	f_buf_release板载缓存(m_生成着色列表);
	f_buf_release板载缓存(m_忽略着色列表);
	f_buf_release板载缓存(m_相交着色列表);
	f_buf_release板载缓存(m_回调着色列表);

	for(auto& e : m_底层加速结构缓存组) f_buf_release板载缓存(e.second);
	for(auto& e : m_顶层加速结构缓存组) f_buf_release板载缓存(e.second);

	f_buf_release板载缓存(m_全局绑定参数指针);
	f_buf_release板载缓存(m_I32动态属性);
	f_buf_release板载缓存(m_F32动态属性);
	f_buf_release板载缓存(m_Vec2动态属性);
	f_buf_release板载缓存(m_Vec3动态属性);

	f_buf_release板载缓存(m_UVec2动态属性);
	f_buf_release板载缓存(m_UVec3动态属性);
	f_buf_release板载缓存(m_UVec4动态属性);

	f_buf_release板载缓存(m_Mat44动态属性);

	f_buf_release板载缓存(m_采样曲线属性);

	f_buf_release板载缓存(m_平行光缓冲区);
	f_buf_release板载缓存(m_点光源缓冲区);
	f_buf_release板载缓存(m_区域光缓冲区);
	f_buf_release板载缓存(m_聚光灯缓冲区);

	f_buf_release板载缓存(m_帧渲染信息);

	f_surface_释放网格(m_区域光物体网格);



	f_tex_销毁纹理(m_渲染纹理);


	for (auto& e : ((S_Vk光追线管*)m_光追管线)->m_着色->m_着色阶段创建信息) {
		vkDestroyShaderModule(((S_Vk光追线管*)m_光追管线)->m_着色->m_设备, e.module, 0);
	}
	vkDestroyPipeline(绘图配置->m_设备, ((S_Vk光追线管*)m_光追管线)->m_线管, nullptr);


	f_vk销毁Fence(绘图配置, m_渲染Fence);
	f_vk销毁命令缓存(绘图配置, (VkCommandBuffer)m_渲染命令缓存);

	
	f_draw_销毁绘图设备(绘图配置);
}

uint32 offset = 0;
void C_Vk光追后端::f_重置() {
	for (auto& e : m_几何间隔) free(e);

	m_几何加速结构.clear();
	m_几何间隔.clear();
	m_几何图元数量.clear();

	//m_底层加速结构缓存组.resize()
}

void C_Vk光追后端::f_添加底层加速结构(S_Mesh* me, uint32 id, E_物体类型 ObType) {
	//auto* 绘图配置 = f_vk_get绘图环境(*m_Ctx);
	auto* 绘图配置 = (S_绘图设备配置*)m_绘图环境;


	VkAccelerationStructureGeometryKHR 几何体{ VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR };
	几何体.flags = VK_GEOMETRY_OPAQUE_BIT_KHR | VK_GEOMETRY_NO_DUPLICATE_ANY_HIT_INVOCATION_BIT_KHR;
	几何体.geometryType = VK_GEOMETRY_TYPE_TRIANGLES_KHR;
	几何体.geometry.triangles.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_TRIANGLES_DATA_KHR;
	几何体.geometry.triangles.vertexFormat = VK_FORMAT_R32G32B32_SFLOAT;
	几何体.geometry.triangles.vertexStride = sizeof(S_VNT1);
	几何体.geometry.triangles.indexType = VK_INDEX_TYPE_UINT32;
	
	几何体.geometry.triangles.vertexData.deviceAddress = f_buf_getAddress(*(me->m_顶点));
	几何体.geometry.triangles.indexData.deviceAddress = f_buf_getAddress(*(me->m_索引));
	几何体.geometry.triangles.maxVertex = me->m_顶点->m_Mem.m_大小;

	assert(me->m_顶点->m_Mem.m_大小 > 0);
	
	

	m_几何图元数量.clear();
	VkAccelerationStructureBuildRangeInfoKHR* 加速体区间绑定 = (VkAccelerationStructureBuildRangeInfoKHR*)malloc(sizeof(VkAccelerationStructureBuildRangeInfoKHR));
	switch (ObType) {
	case E_物体类型::t_多边形: 
	case E_物体类型::t_网格: {
		uint32 tnum = me->m_索引->m_Mem.m_大小 / 3;
		assert(tnum > 1);
		加速体区间绑定->firstVertex = 0;
		加速体区间绑定->primitiveCount = tnum;
		加速体区间绑定->primitiveOffset = 0;
		加速体区间绑定->transformOffset = 0;

		m_几何图元数量.push_back(tnum);
		break;
	}
	case E_物体类型::t_区域光_方形: {
		加速体区间绑定->firstVertex = 0;
		加速体区间绑定->primitiveCount = 2;
		加速体区间绑定->primitiveOffset = 0;
		加速体区间绑定->transformOffset = 0;
		m_几何图元数量.push_back(2);
		break;
	}
	case E_物体类型::t_区域光_圆形: {
		加速体区间绑定->firstVertex = 4;
		加速体区间绑定->primitiveCount = 128;
		加速体区间绑定->primitiveOffset = 0;
		加速体区间绑定->transformOffset = 0;
		m_几何图元数量.push_back(128);
		break;
	}
	}



	if (id >= m_场景物体网格指针->m_Size) f_buf_resize(m_场景物体网格指针, id + 1);
	S_ObMesh mesh = {
		f_buf_getAddress(*me->m_顶点),
		f_buf_getAddress(*me->m_UV),
		f_buf_getAddress(*me->m_UV索引),
		f_buf_getAddress(*me->m_索引),
		f_buf_getAddress(*me->m_面),
		DEF_ObType_Mesh,
		0,
	};
	f_buf_set<S_ObMesh>(m_场景物体网格指针, id, mesh);


	S_Geom geom = me->m_几何属性;
	geom.boundSize = me->m_Bound.max - me->m_Bound.min;
	geom.boundOffset = me->m_Bound.min;
	
	if (me->m_UV->m_Mem.m_大小) {
		geom.uvLayerOffset = me->m_UV->m_Mem.m_偏移;
	}
	else {
		geom.uvLayerNum = 0;
		geom.uvLayerOffset = 0;
	}
	if (id >= m_几何属性->m_Size) f_buf_resize(m_几何属性, id + 1);
	f_buf_set<S_Geom>(m_几何属性, (uint64)id, geom);



	
	m_加速结构绑定几何信息 = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR };
	m_加速结构绑定几何信息.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR;
	m_加速结构绑定几何信息.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR | VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_UPDATE_BIT_KHR;
	m_加速结构绑定几何信息.geometryCount = 1;
	m_加速结构绑定几何信息.pGeometries = &几何体;

	m_加速结构绑定大小信息 = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_SIZES_INFO_KHR };
	绘图配置->vkGetAccelerationStructureBuildSizes(绘图配置->m_设备,
		VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR,
		&m_加速结构绑定几何信息,
		m_几何图元数量.data(),
		&m_加速结构绑定大小信息);


	S_板载缓存* BLAS;
	if (m_底层加速结构缓存组.find(id) == m_底层加速结构缓存组.end()) {
		BLAS = f_buf_create板载缓存(*m_Ctx, 1, E_板载缓存类型::e_光追底层加速结构, 1);
		m_底层加速结构缓存组[id] = BLAS;
	}
	else {
		BLAS = m_底层加速结构缓存组[id];
	}

	//if (!me->m_底层光追加速结构->m_Buf) {
	//}
	f_buf_gresize(*me->m_底层光追加速结构, m_加速结构绑定大小信息.accelerationStructureSize);

	f_buf_resize(BLAS, m_加速结构绑定大小信息.accelerationStructureSize);
	f_buf_resize(m_底层暂存缓冲区, m_加速结构绑定大小信息.accelerationStructureSize);

	m_加速结构绑定几何信息.mode = VK_BUILD_ACCELERATION_STRUCTURE_MODE_BUILD_KHR;
	m_加速结构绑定几何信息.dstAccelerationStructure = f_buf_get加速结构(BLAS);
	m_加速结构绑定几何信息.scratchData.deviceAddress = f_buf_getAddress(static_cast<S_VkArray缓存*>(m_底层暂存缓冲区));

	f_Draw_Begin(m_Ctx, m_加速结构创建命令缓存, m_渲染Fence);
	绘图配置->vkCmdBuildAccelerationStructures((VkCommandBuffer)m_加速结构创建命令缓存, 1, &m_加速结构绑定几何信息, &加速体区间绑定);
	f_Draw_end(m_Ctx, m_加速结构创建命令缓存, m_渲染Fence, 3);


	free(加速体区间绑定);
}

void C_Vk光追后端::f_清除几何加速结构() {
	for (auto& e : m_底层加速结构缓存组) {
		f_buf_release板载缓存(e.second);
	}
	m_底层加速结构缓存组.clear();
}

void C_Vk光追后端::f_分配底层加速结构数量(uint32 num) {
	auto* 绘图配置 = f_vk_get绘图环境(*m_Ctx);

}

void C_Vk光追后端::f_更新实例变换(mat3X4 mat, uint32 offset, uint32 底层ASID, uint8 DataID, uint8 mask) {
	//auto* 绘图配置 = f_vk_get绘图环境(*m_Ctx);
	auto* 绘图配置 = (S_绘图设备配置*)m_绘图环境;


	if (m_底层加速结构缓存组.find(底层ASID) == m_底层加速结构缓存组.end()) {
		std::cout<<"错误:底层加速结果没当前ID物体 "<<底层ASID<<std::endl;
		return;
	}

	if (offset >= m_顶层实例加速结构->m_Size) f_buf_resize(m_顶层实例加速结构, offset + 1);
	auto ptr_instance = f_buf_map板载缓存<VkAccelerationStructureInstanceKHR>(m_顶层实例加速结构);
	ptr_instance[offset].transform = *((VkTransformMatrixKHR*)&mat);
	ptr_instance[offset].instanceCustomIndex = 底层ASID | uint32(DataID << 16);
	ptr_instance[offset].mask = mask;
	ptr_instance[offset].instanceShaderBindingTableRecordOffset = 0;
	ptr_instance[offset].flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR;
	ptr_instance[offset].accelerationStructureReference = f_buf_getAddress(static_cast<S_VkArray缓存*>(m_底层加速结构缓存组[底层ASID]));
	f_buf_unmap板载缓存(m_顶层实例加速结构);

	return;
}


void C_Vk光追后端::f_构建顶层加速结构(uint32 num) {
	//auto* 绘图配置 = f_vk_get绘图环境(*m_Ctx);
	auto* 绘图配置 = (S_绘图设备配置*)m_绘图环境;


	S_GlobalParam 全局参数 = {
		f_buf_getAddress(static_cast<S_VkArray缓存*>(m_I32动态属性)),
		f_buf_getAddress(static_cast<S_VkArray缓存*>(m_F32动态属性)),
		f_buf_getAddress(static_cast<S_VkArray缓存*>(m_Vec2动态属性)),
		f_buf_getAddress(static_cast<S_VkArray缓存*>(m_Vec3动态属性)),

		f_buf_getAddress(static_cast<S_VkArray缓存*>(m_UVec2动态属性)),
		f_buf_getAddress(static_cast<S_VkArray缓存*>(m_UVec3动态属性)),
		f_buf_getAddress(static_cast<S_VkArray缓存*>(m_UVec4动态属性)),

		f_buf_getAddress(static_cast<S_VkArray缓存*>(m_Mat44动态属性)),

		f_buf_getAddress(static_cast<S_VkArray缓存*>(m_采样曲线属性)),

		f_buf_getAddress(static_cast<S_VkArray缓存*>(m_平行光缓冲区)),
		f_buf_getAddress(static_cast<S_VkArray缓存*>(m_点光源缓冲区)),
		f_buf_getAddress(static_cast<S_VkArray缓存*>(m_区域光缓冲区)),
		f_buf_getAddress(static_cast<S_VkArray缓存*>(m_聚光灯缓冲区)),

		f_buf_getAddress(static_cast<S_VkArray缓存*>(m_材质)),
	};
	f_buf_set<S_GlobalParam>(m_全局绑定参数指针, 0, 全局参数);


	
	m_mapPtr_渲染帧数据->m_PointLightNum = m_点光源缓冲区->m_Size;
	
	//uint32 实例数量 = DEF_Min(10, num);
	uint32 实例数量 = num;
	if (实例数量) {
		VkDeviceOrHostAddressConstKHR 实例数据{};
		实例数据.deviceAddress = f_buf_getAddress(m_顶层实例加速结构);
		m_顶层几何实例加速结构.clear();
		VkAccelerationStructureGeometryKHR asg{ VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR }; {
			asg.geometryType = VK_GEOMETRY_TYPE_INSTANCES_KHR;
			asg.flags = VK_GEOMETRY_OPAQUE_BIT_KHR;
			//asg.flags = VK_GEOMETRY_NO_DUPLICATE_ANY_HIT_INVOCATION_BIT_KHR;
			asg.geometry.instances.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_INSTANCES_DATA_KHR;
			asg.geometry.instances.arrayOfPointers = VK_FALSE;
			asg.geometry.instances.data = 实例数据;
		}
		m_顶层几何实例加速结构.push_back(asg);



		m_加速结构绑定几何实例信息 = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR };
		m_加速结构绑定几何实例信息.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR;
		m_加速结构绑定几何实例信息.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
		m_加速结构绑定几何实例信息.geometryCount = m_顶层几何实例加速结构.size();
		m_加速结构绑定几何实例信息.pGeometries = m_顶层几何实例加速结构.data();



		uint32_t primitive_count = 实例数量;
		m_加速结构绑定实例大小信息 = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_SIZES_INFO_KHR };
		绘图配置->vkGetAccelerationStructureBuildSizes(
			绘图配置->m_设备,
			VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR,
			&m_加速结构绑定几何实例信息,
			&primitive_count,
			&m_加速结构绑定实例大小信息);

		f_buf_resize(m_顶层加速结构,   m_加速结构绑定实例大小信息.accelerationStructureSize);
		f_buf_resize(m_顶层暂存缓冲区, m_加速结构绑定实例大小信息.accelerationStructureSize);

		m_加速结构绑定几何实例信息.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR;
		m_加速结构绑定几何实例信息.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
		m_加速结构绑定几何实例信息.mode = VK_BUILD_ACCELERATION_STRUCTURE_MODE_BUILD_KHR;
		//m_加速结构绑定几何实例信息.mode = VK_BUILD_ACCELERATION_STRUCTURE_MODE_UPDATE_KHR;
		m_加速结构绑定几何实例信息.dstAccelerationStructure = f_buf_get加速结构(m_顶层加速结构);
		m_加速结构绑定几何实例信息.scratchData.deviceAddress = f_buf_getAddress(static_cast<S_VkArray缓存*>(m_顶层暂存缓冲区));


		m_实例区间.clear();
		VkAccelerationStructureBuildRangeInfoKHR 实例区间{};
		实例区间.primitiveCount = 实例数量;
		实例区间.primitiveOffset = 0;
		实例区间.firstVertex = 0;
		实例区间.transformOffset = 0;
		m_实例区间.push_back(&实例区间);



		f_Draw_Begin(m_Ctx, m_加速结构创建命令缓存, m_渲染Fence);
		绘图配置->vkCmdBuildAccelerationStructures((VkCommandBuffer)m_加速结构创建命令缓存, 1, &m_加速结构绑定几何实例信息, m_实例区间.data());
		f_Draw_end(m_Ctx, m_加速结构创建命令缓存, m_渲染Fence, S_光追后端::g_光追列队);
		return;
	}
	else {
		m_加速结构绑定几何实例信息 = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR };
		m_加速结构绑定几何实例信息.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR;
		m_加速结构绑定几何实例信息.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
		m_加速结构绑定几何实例信息.geometryCount = 0;
		m_加速结构绑定几何实例信息.pGeometries = nullptr;
	}
	

	f_Draw_Begin(m_Ctx, m_加速结构创建命令缓存, m_渲染Fence);
	绘图配置->vkCmdBuildAccelerationStructures((VkCommandBuffer)m_加速结构创建命令缓存, 0, &m_加速结构绑定几何实例信息, m_实例区间.data());
	f_Draw_end(m_Ctx, m_加速结构创建命令缓存, m_渲染Fence, 3);


}

void C_Vk光追后端::f_构建着色绑定列表() {
	//auto* 绘图配置 = f_vk_get绘图环境(*m_Ctx);
	auto* 绘图配置 = (S_绘图设备配置*)m_绘图环境;


	auto rt = static_cast<S_Vk光追线管*>(m_光追管线);
	uint32 着色器数量 = rt->m_着色->m_光追着色器.size();

	uint32 着色组列表大小 = m_光追管线属性.shaderGroupHandleSize;
	//uint32 着色器绑定列表大小 = 着色器数量 * 着色组列表大小;

	
	
	uint32 光线生成对齐跨度 = f_shader_着色组对齐大小(着色组列表大小, m_光追管线属性.shaderGroupHandleAlignment);


	
	

	std::vector<uint32> 着色器阶段数量(4);
	std::vector<uint32> 着色器列表阶段偏移(4);
	uint32 offset = 0;
	for (uint32 i = 0; i < 着色器数量; ++i) {
		switch (rt->m_着色->m_光追着色器[i].m_Type) {
		case E_着色阶段::e_光追射线生成:
			if(!着色器列表阶段偏移[E_光追阶段ID::e_光追阶段ID_光线生成]) 着色器列表阶段偏移[E_光追阶段ID::e_光追阶段ID_光线生成] = offset;
			++着色器阶段数量[E_光追阶段ID::e_光追阶段ID_光线生成];
			break;
		case E_着色阶段::e_光追忽略相交:
			if(!着色器列表阶段偏移[E_光追阶段ID::e_光追阶段ID_光线忽略]) 着色器列表阶段偏移[E_光追阶段ID::e_光追阶段ID_光线忽略] = offset;
			++着色器阶段数量[E_光追阶段ID::e_光追阶段ID_光线忽略];
			break;
		case E_着色阶段::e_光追最近相交:
			if(!着色器列表阶段偏移[E_光追阶段ID::e_光追阶段ID_最近相交]) 着色器列表阶段偏移[E_光追阶段ID::e_光追阶段ID_最近相交] = offset;
			++着色器阶段数量[E_光追阶段ID::e_光追阶段ID_最近相交];
			break;
		case E_着色阶段::e_光追回调:
			if(!着色器列表阶段偏移[E_光追阶段ID::e_光追阶段ID_材质回调]) 着色器列表阶段偏移[E_光追阶段ID::e_光追阶段ID_材质回调] = offset;
			++着色器阶段数量[E_光追阶段ID::e_光追阶段ID_材质回调];
			break;
		default:
			break;
		}
		++offset;
	}
	

	//offset = 1 + 着色器阶段数量[E_光追阶段ID::e_光追阶段ID_光线忽略] + 着色器阶段数量[E_光追阶段ID::e_光追阶段ID_最近相交] + 着色器阶段数量[E_光追阶段ID::e_光追阶段ID_材质回调];
	offset = 着色器数量 * 着色组列表大小;

	std::vector<uint8> shaderHandleStorage(offset);
	VkResult err = 绘图配置->vkGetRayTracingShaderGroupHandles(绘图配置->m_设备, rt->m_线管, 0, 着色器数量, offset, shaderHandleStorage.data());
	
	
	uint32 回调数量 = 着色器阶段数量[E_光追阶段ID::e_光追阶段ID_材质回调];
	uint32 对齐跨度 = f_shader_着色组对齐大小(m_光追管线属性.shaderGroupHandleSize, m_光追管线属性.shaderGroupHandleAlignment);

	uint32 光线生成列表大小 = f_shader_着色组对齐大小(对齐跨度 * 着色器阶段数量[E_光追阶段ID::e_光追阶段ID_光线生成], m_光追管线属性.shaderGroupBaseAlignment);
	uint32 光线忽略列表大小 = f_shader_着色组对齐大小(对齐跨度 * 着色器阶段数量[E_光追阶段ID::e_光追阶段ID_光线忽略], m_光追管线属性.shaderGroupBaseAlignment);
	uint32 光线相交列表大小 = f_shader_着色组对齐大小(对齐跨度 * 着色器阶段数量[E_光追阶段ID::e_光追阶段ID_最近相交], m_光追管线属性.shaderGroupBaseAlignment);
	uint32 光线回调列表大小 = f_shader_着色组对齐大小(对齐跨度 * 回调数量, m_光追管线属性.shaderGroupBaseAlignment);

	
	
	f_buf_resize(m_生成着色列表, 光线生成列表大小);
	f_buf_resize(m_忽略着色列表, 光线忽略列表大小);
	f_buf_resize(m_相交着色列表, 光线相交列表大小);
	
	f_buf_set(m_生成着色列表, 0);
	f_buf_set(m_忽略着色列表, 0);
	f_buf_set(m_相交着色列表, 0);
	

	offset = 0;
	f_buf_fill(m_生成着色列表, 0, &shaderHandleStorage[0], 对齐跨度);
	offset += 光线生成列表大小;
	f_buf_fill(m_忽略着色列表, 0, &shaderHandleStorage[对齐跨度], 对齐跨度);
	offset += 光线忽略列表大小;
	f_buf_fill(m_相交着色列表, 0, &shaderHandleStorage[对齐跨度 * 2], 对齐跨度);

	offset = 0;
	if (回调数量) {
		f_buf_resize(m_回调着色列表, 光线生成列表大小 * 回调数量);
		
		for (uint32 i = 0; i < 回调数量; ++i) {
			f_buf_fill(m_回调着色列表, offset, &shaderHandleStorage[对齐跨度 * (3+i)], 对齐跨度);

			offset += 光线生成列表大小;
		}
		
	}



	auto 地址 = f_shader_get着色绑定列表跨度设备地址((S_VkArray缓存*)m_生成着色列表, 光线生成列表大小, 光线生成列表大小, 0);
	m_光追生成GPU地址 = 地址;

	地址 = f_shader_get着色绑定列表跨度设备地址((S_VkArray缓存*)m_忽略着色列表, 光线生成列表大小, 光线生成列表大小, 0);
	m_光追忽略GPU地址 = 地址;

	地址 = f_shader_get着色绑定列表跨度设备地址((S_VkArray缓存*)m_相交着色列表, 光线生成列表大小, 光线生成列表大小, 0);
	m_光追相交GPU地址 = 地址;

	if (回调数量) {
		地址 = f_shader_get着色绑定列表跨度设备地址((S_VkArray缓存*)m_回调着色列表, 光线生成列表大小, 光线生成列表大小*回调数量, 0);
		m_光追回调GPU地址 = 地址;
	}
	else {
		m_光追回调GPU地址 = {};
	}
	


	rt->m_着色->f_alloc参数设置((S_结构对象指针*)&m_GPU参数设置);
}

void C_Vk光追后端::f_构建BVH() {
	auto* 绘图配置 = f_vk_get绘图环境(*m_Ctx);

}

void C_Vk光追后端::f_填充平行光参数(std::vector<S_LigthParallel>& 平行光参数组) {
	if (平行光参数组.size()) {
		f_buf_resize(m_平行光缓冲区, 平行光参数组.size());
		f_buf_fill(m_平行光缓冲区, 平行光参数组.data(), 平行光参数组.size());
	}
	
	m_mapPtr_渲染帧数据->m_ParallelLightNum = 平行光参数组.size();
	
}

void C_Vk光追后端::f_Rander(uvec2 viewSize) {
	f_递增渲染帧记录();

	//auto* 绘图配置 = f_vk_get绘图环境(*m_Ctx);
	auto* 绘图配置 = (S_绘图设备配置*)m_绘图环境;

	//auto rt = static_cast<S_Vk光追线管*>(m_光追管线);
	VkDescriptorSet DS = (VkDescriptorSet)m_GPU参数设置;

	f_Draw_Begin(m_Ctx, m_渲染命令缓存, m_渲染Fence);

	vkCmdBindPipeline((VkCommandBuffer)m_渲染命令缓存, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, static_cast<S_Vk光追线管*>(m_光追管线)->m_线管);
	vkCmdBindDescriptorSets((VkCommandBuffer)m_渲染命令缓存, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, static_cast<S_Vk光追线管*>(m_光追管线)->m_线管布局, 0, 1, &DS, 0, 0);

	VkStridedDeviceAddressRegionKHR emptySbtEntry = {};
	/*绘图配置->vkCmdTraceRays((VkCommandBuffer)m_渲染命令缓存,
		&(rt->m_着色->m_光追着色器[0].m_跨度设备指针),
		&(rt->m_着色->m_光追着色器[1].m_跨度设备指针),
		m_光追相交GPU地址.data(),
		m_光追回调GPU地址.data(),
		viewSize.x,
		viewSize.y,
		1
	);*/
	绘图配置->vkCmdTraceRays((VkCommandBuffer)m_渲染命令缓存,
		&m_光追生成GPU地址,
		&m_光追忽略GPU地址,
		&m_光追相交GPU地址,
		&m_光追回调GPU地址,
		//&emptySbtEntry,
		viewSize.x,
		viewSize.y,
		1
	);

	f_Draw_end(m_Ctx, m_渲染命令缓存, m_渲染Fence, 3);

	
	
}

void C_Vk光追后端::f_Rander(uvec2 viewSize, uint32 采样次数) {
	auto* 绘图配置 = (S_绘图设备配置*)m_绘图环境;

		//auto rt = static_cast<S_Vk光追线管*>(m_光追管线);
	VkDescriptorSet DS = (VkDescriptorSet)m_GPU参数设置;

	for (uint32 i = 0; i < 采样次数; ++i) {
		f_递增渲染帧记录();

		f_Draw_Begin(m_Ctx, m_渲染命令缓存, m_渲染Fence);

		vkCmdBindPipeline((VkCommandBuffer)m_渲染命令缓存, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, static_cast<S_Vk光追线管*>(m_光追管线)->m_线管);
		vkCmdBindDescriptorSets((VkCommandBuffer)m_渲染命令缓存, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, static_cast<S_Vk光追线管*>(m_光追管线)->m_线管布局, 0, 1, &DS, 0, 0);

		VkStridedDeviceAddressRegionKHR emptySbtEntry = {};
		
		绘图配置->vkCmdTraceRays((VkCommandBuffer)m_渲染命令缓存,
			&m_光追生成GPU地址,
			&m_光追忽略GPU地址,
			&m_光追相交GPU地址,
			&m_光追回调GPU地址,
			//&emptySbtEntry,
			viewSize.x,
			viewSize.y,
			1
		);

		f_Draw_end(m_Ctx, m_渲染命令缓存, m_渲染Fence, 3);

		if (采样次数) {
			std::cout<<"完成渲染采样:"<< 采样次数<<"/"<<i<<std::endl;
		}

		
		if(!S_光追后端::g_渲染状态) break;
	}
}

void C_Vk光追后端::f_重构材质线管() {
	auto 绘图配置 = f_vk_get绘图环境(*m_Ctx);

	vkDestroyPipeline(绘图配置->m_设备, ((S_Vk光追线管*)m_光追管线)->m_线管, nullptr);
	//((S_Vk光追线管*)m_光追管线)->m_着色->f_free参数设置((S_结构对象指针*)&m_GPU参数设置);
	((S_Vk光追线管*)m_光追管线)->m_着色->f_initRT着色参数布局(m_线管创建配置信息);
	//vkDestroyPipelineLayout(绘图配置->m_设备, m_线管布局, nullptr);
	((S_Vk光追线管*)m_光追管线)->m_线管 = f_pipeline_创建(((S_Vk光追线管*)m_光追管线)->m_着色, ((S_Vk光追线管*)m_光追管线)->m_线管布局, *m_Ctx);

	f_构建着色绑定列表();
}

void C_Vk光追后端::f_重载() {
	//auto 绘图配置 = f_vk_get绘图环境(*m_Ctx);
	auto* 绘图配置 = (S_绘图设备配置*)m_绘图环境;


	for (auto& e : ((S_Vk光追线管*)m_光追管线)->m_着色->m_着色阶段创建信息) {
		vkDestroyShaderModule(((S_Vk光追线管*)m_光追管线)->m_着色->m_设备, e.module, 0);
	}
	vkDestroyPipeline(绘图配置->m_设备, ((S_Vk光追线管*)m_光追管线)->m_线管, nullptr);

	((S_Vk光追线管*)m_光追管线)->m_线管 = f_pipeline_创建(((S_Vk光追线管*)m_光追管线)->m_着色, ((S_Vk光追线管*)m_光追管线)->m_线管布局, *m_Ctx);
	f_构建着色绑定列表();
}

void C_Vk光追后端::f_add纹理(S_纹理* 纹理, uint32 loc) {
	if (loc >= m_纹理信息.size()) {
		//int32 已有数量 = m_纹理信息.size();
		m_纹理信息.resize(loc+1);
		//m_纹理绑定计数 += m_纹理信息.size() - 已有数量;
	}
	m_纹理信息[loc] = ((S_VkImage*)纹理)->m_着色器参数;
}

void C_Vk光追后端::f_set纹理(S_纹理* 纹理) {
	m_纹理信息[纹理->m_TexID] = ((S_VkImage*)纹理)->m_着色器参数;
}

void C_Vk光追后端::f_rem纹理(uint32 loc) {
	if (loc < m_纹理信息.size()) {
		m_纹理信息.erase(m_纹理信息.begin() + loc);
	}
}




void f_bd_安装光追材质着色器(S_光追后端* rt, int32 loc, std::string path, uint32 光追ID) {
	if (f_file_文件是否存在(path)) {
		auto* pipeline = ((S_Vk光追线管*)rt->m_光追管线);

		光追ID += E_光追阶段ID::e_光追阶段ID_材质回调;

		loc += E_光追阶段ID::e_光追阶段ID_材质回调;

		uint32 num = pipeline->m_着色->m_光追着色组创建信息.size();
		for (uint32 i = 0; i < num; ++i) {
			const VkRayTracingShaderGroupCreateInfoKHR& 光追着色组 = pipeline->m_着色->m_光追着色组创建信息[i];
			if (光追着色组.generalShader == 光追ID) {
				//loc = i;
				break;
			}
		}

		if (pipeline->m_着色->m_光追着色器.size() <= loc) {
			for (uint32 i = pipeline->m_着色->m_光追着色器.size(); i <= loc; ++i) {
				pipeline->m_着色->m_着色阶段创建信息.push_back({});

				pipeline->m_着色->m_光追着色器.push_back({ E_着色阶段::e_光追回调 });
				pipeline->m_着色->m_光追着色组创建信息.push_back({});
			}
		}
		else {
			f_vkShader_销毁着色器(pipeline->m_着色, loc, true);
		}

		if (pipeline->m_着色->m_光追着色器.size() <= loc) {
			std::cout << "fuck" << std::endl;
			rt;
		}
		f_vkShader_创建着色器(pipeline->m_着色, path, E_着色阶段::e_光追回调, loc, true, 光追ID);

		rt->m_重新编译线管 = true;
	}
}

void f_bd_删除光追材质着色器(S_光追后端* rt, int32 loc) {
	auto* pipeline = ((S_Vk光追线管*)rt->m_光追管线);
	loc += E_光追阶段ID::e_光追阶段ID_材质回调;

	pipeline->f_销毁着色器(loc);
	rt->m_重新编译线管 = true;
}


void f_df_构建顶层光追加速(S_设备环境& ctx, std::vector<S_GPU内存块索引>& 光追实例加速结构, S_GPU内存块索引& 顶层加速结构) {
	S_绘图设备配置* 绘图配置 = (S_绘图设备配置*)f_df_get绘图环境(ctx);

	uint32 num = 光追实例加速结构.size();
	uint32 实例数量 = 0;


	std::vector<VkAccelerationStructureGeometryKHR> m_顶层几何实例加速结构(num);
	for (uint32 i = 0; i < num; ++i) {
		实例数量 += 光追实例加速结构[i].m_Mem.m_大小;

		VkDeviceOrHostAddressConstKHR 实例数据{};
		实例数据.deviceAddress = f_buf_getAddress(光追实例加速结构[i]);


		VkAccelerationStructureGeometryKHR asg{ VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR }; {
			asg.geometryType = VK_GEOMETRY_TYPE_INSTANCES_KHR;
			asg.flags = VK_GEOMETRY_OPAQUE_BIT_KHR;
			//asg.flags = VK_GEOMETRY_NO_DUPLICATE_ANY_HIT_INVOCATION_BIT_KHR;
			asg.geometry.instances.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_INSTANCES_DATA_KHR;
			asg.geometry.instances.arrayOfPointers = VK_FALSE;
			asg.geometry.instances.data = 实例数据;
		}

		m_顶层几何实例加速结构[i] = asg;
	}


	if (实例数量) {
		VkAccelerationStructureBuildGeometryInfoKHR m_加速结构绑定几何实例信息 = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR };
		m_加速结构绑定几何实例信息.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR;
		m_加速结构绑定几何实例信息.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
		m_加速结构绑定几何实例信息.geometryCount = 1;
		m_加速结构绑定几何实例信息.pGeometries = m_顶层几何实例加速结构.data();


		uint32_t primitive_count = 实例数量;
		VkAccelerationStructureBuildSizesInfoKHR m_加速结构绑定实例大小信息 = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_SIZES_INFO_KHR };
		绘图配置->vkGetAccelerationStructureBuildSizes(
			绘图配置->m_设备,
			VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR,
			&m_加速结构绑定几何实例信息,
			&primitive_count,
			&m_加速结构绑定实例大小信息);

		auto* m_顶层暂存缓冲区 = f_buf_create板载缓存(
			ctx, 
			E_板载缓存类型::e_SSBO, 
			E_板载缓存类型::e_SSBO | E_板载缓存类型::e_着色端内存地址, 
			1, 
			m_加速结构绑定实例大小信息.accelerationStructureSize
		);
		f_buf_gresize(顶层加速结构, m_加速结构绑定实例大小信息.accelerationStructureSize);


		m_加速结构绑定几何实例信息.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR;
		m_加速结构绑定几何实例信息.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
		m_加速结构绑定几何实例信息.mode = VK_BUILD_ACCELERATION_STRUCTURE_MODE_BUILD_KHR;
		m_加速结构绑定几何实例信息.dstAccelerationStructure = (VkAccelerationStructureKHR)f_buf_get加速结构(顶层加速结构);
		m_加速结构绑定几何实例信息.scratchData.deviceAddress = f_buf_getAddress(static_cast<S_VkArray缓存*>(m_顶层暂存缓冲区));


		VkAccelerationStructureBuildRangeInfoKHR 实例区间;
		实例区间.primitiveCount = 实例数量;
		实例区间.primitiveOffset = 0;
		实例区间.firstVertex = 0;
		实例区间.transformOffset = 0;
		const VkAccelerationStructureBuildRangeInfoKHR* 绑定偏移信息 = &实例区间;


		f_Draw_Begin(&ctx, S_光追后端::m_加速结构创建命令缓存, S_光追后端::m_渲染Fence);
		绘图配置->vkCmdBuildAccelerationStructures((VkCommandBuffer)S_光追后端::m_加速结构创建命令缓存, 1, &m_加速结构绑定几何实例信息, &绑定偏移信息);
		f_Draw_end(&ctx, S_光追后端::m_加速结构创建命令缓存, S_光追后端::m_渲染Fence, 3);
	}
}








